/**
  ******************************************************************************
  * @file    common.h
  * @author  software Team
  * @brief   This file provides all the headers of the common functions.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2020 Hyseim. Co., Ltd.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __COMMON_H__
#define __COMMON_H__

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "utils.h"

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/

//TODO
#define POLLING_FREQ            32000       //32K clock timer
#define CUR_TIMER_CNT           READ_REG(0) //this should be the timer cnt reg value

#define RT_POLLING_BY_TIME(src, des, msec, ret) do{\
    uint32_t cnt = CUR_TIMER_CNT;\
    uint32_t interval = (msec) * (POLLING_FREQ / 1000);\
    (ret) = TEST_FAILD;\
    while (CUR_TIMER_CNT - cnt) < interval) {\
        if ((src) == (des)) {\
            (ret) = TEST_OK;\
            break;\
        }\
    }\
}while(0);

#define RT_POLLING_BY_CNT(src, des, cnt, ret) do{\
    volatile uint32_t counter = (cnt);\
    (ret) = TEST_FAILD;\
    while (counter--) {\
        if((src) == (des)){\
            (ret) = TEST_OK;\
            break;\
        }\
    }\
}while(0);


#define ABS(A)    (((A) >= 0  ) ? (A) : -(A))  // ¾ø¶ÔÖµº¯Êý
#define MIN(A,B)  (((A) <  (B)) ? (A) : (B))   // Çó×îÐ¡º¯Êý
#define MAX(A,B)  (((A) >  (B)) ? (A) : (B))   // Çó×î´óº¯Êý

#define disable_irq()           CSRCEX(CSR_MSTATUS, BIT(3))
#define enable_irq()            CSRSEX(CSR_MSTATUS, BIT(3))
/* Exported functions ------------------------------------------------------- */

// common delay function;
// for DV case, m should be set to less than 100;
// for FPGA case, m could be set to more than 100;
void mdelay(uint32_t m);
// us delay @ SoC_clk=200MHz
void udelay(int m);

#endif  /* __COMMON_H */

/*******************(C)COPYRIGHT 2020 Hyseim ***********END OF FILE************/

